import { Component, OnInit } from '@angular/core';
import { TaskService, TaskListOption, TaskStatus } from 'app/modules/task/services/task.service';
import { BehaviorSubject } from 'rxjs';
import { switchMap, scan, map } from 'rxjs/operators';
import _ from 'lodash';

@Component({
  selector: 'm-task',
  templateUrl: './task.component.html',
  styleUrls: ['./task.component.scss'],
})
export class TaskComponent implements OnInit {
  value = 'today';
  tabs = [
    {
      label: '今日',
      value: 'today',
    },
    {
      label: '本周',
      value: 'month',
    },
    {
      label: '本月',
      value: 'year',
    },
  ];
  constructor(private taskService: TaskService) {}
  TaskStatus = TaskStatus;
  query$ = new BehaviorSubject<TaskListOption>({ category: 'all', since: 'today', count: 5 });
  tasks$ = this.query$.pipe(
    scan((opt, v) => ({ ...opt, ...v })),
    switchMap((opt) => this.taskService.list(opt)),
    map((result) => {
      result.list.forEach((t) => {
        let zones = [];
        let areas = _.chain(t).get('jobs.0.areas').value();
        if (areas.length) {
          areas.forEach((item) => {
            zones.push(item.name);
          });
        }
        t['zones'] = zones;
      });
      return result;
    }),
  );

  ngOnInit() {}

  timeChange(value) {
    this.query$.next({ category: 'all', since: value, page: 1 });
  }
  pageChange(page: number) {
    this.query$.next({ category: 'all', page });
  }
}
